package com.deli.pagingsimple;

import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.paging.LoadState;
import androidx.recyclerview.widget.RecyclerView;

import com.deli.pagingforjava.adapter.BaseFooterAdapter;

import org.jetbrains.annotations.NotNull;

/**
 * @Author: QDL
 * @CreateDate: 2021/4/25 14:22
 */
public class FooterViewAdapter extends BaseFooterAdapter<FooterViewAdapter.FooterViewHolder> {
    LayoutInflater layoutInflater;
    AnimationDrawable animationDrawable;

    public FooterViewAdapter(LayoutInflater layoutInflater) {
        super(layoutInflater);
        this.layoutInflater = layoutInflater;
    }


    @Override
    public void onBindViewHolder(@NotNull FooterViewHolder footerViewHolder, @NotNull LoadState loadState) {
        animationDrawable = (AnimationDrawable) footerViewHolder.ivAnim.getDrawable();
        if (loadState instanceof LoadState.Loading) {
            animationDrawable.start();
        } else if (loadState instanceof LoadState.Error) {
            animationDrawable.stop();
        }
    }

    @NotNull
    @Override
    public FooterViewHolder onCreateViewHolder(@NotNull ViewGroup viewGroup, @NotNull LoadState loadState) {
        return new FooterViewHolder(layoutInflater.inflate(R.layout.load_more_view, viewGroup, false));
    }

    public static class FooterViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAnim;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAnim = itemView.findViewById(R.id.iv_anim);
        }
    }
}
